
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class FamilyAdd extends JFrame{
    public FamilyAdd(String userid){

        SQLOperation sql=new SQLOperation();

        setTitle("Family Add");
        Container container=getContentPane();
        setLayout(new BorderLayout());

        JPanel titlePanel=new JPanel();

        JLabel title=new JLabel("Please complete the following information.");
        title.setFont(new Font("",Font.BOLD,20));

        titlePanel.add(title);
        add(titlePanel,BorderLayout.NORTH);

        JPanel info=new JPanel(new GridLayout(2,1,5,5));

        JPanel familyPanel=new JPanel();

        familyPanel.add(new JLabel("Family Name:"));
        JTextField fName=new JTextField(18);
        familyPanel.add(fName);
        info.add(familyPanel);

        JPanel addressPanel=new JPanel();

        addressPanel.add(new JLabel("        Address:"));
        JTextField address=new JTextField(18);
        addressPanel.add(address);
        info.add(addressPanel);

        add(info,BorderLayout.CENTER);


        JButton back=new JButton("Back");
        back.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                new FamilyList(userid);
            }
        });

        JButton add=new JButton("Add");
        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(fName.getText().equals("")||address.getText().equals("")){
                    JOptionPane.showMessageDialog
                            (null, "Please Fill in all information!","Missing Input",
                                    JOptionPane.ERROR_MESSAGE);

                }
                else{

                    String newPK="";
                    boolean isSame=true;
                    while(isSame){

                        isSame=false;
                        newPK="fi";


                        char j;
                        for(int i=1;i<=5;i++){
                            j=(char)(Math.random()*10+48);
                            newPK=newPK+""+j;

                        }
                        String[] pkID={"familyID"};
                        Object[][] pk=sql.search("Family",pkID,"shanchu",1);
                        for(int i=0;i<pk.length;i++){
                            if(pk[i][0].equals(newPK)){
                                isSame=true;
                                break;
                            }
                        }
                    }




                    Object[] newInfo={newPK,fName.getText(),address.getText(),1};

                    sql.add(newInfo,"Family");


                    JOptionPane.showMessageDialog
                            (null, "Add Complete!Random familyID:"+newPK,
                                    "Success",JOptionPane.INFORMATION_MESSAGE);

                    setVisible(false);

                    new FamilyList(userid);


                }


            }
        });




        JPanel buttons=new JPanel();
        buttons.add(back);
        buttons.add(add);

        add(buttons,BorderLayout.SOUTH);






        setSize(500,300);
        setResizable(false);
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        setVisible(true);




    }
}
